package com.yunquan.ohana.ui.calendar.event.dialog

import android.content.Intent
import android.view.Gravity
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.ruffian.library.widget.RCheckBox
import com.yunquan.ohana.R
import com.yunquan.ohana.adapter.ModelRecyclerAdapter
import com.yunquan.ohana.adapter.ModelViewHolder
import com.yunquan.ohana.base.dialog.BaseDialog
import com.yunquan.ohana.databinding.DialogSyncToDeviceBinding
import com.yunquan.ohana.entity.FramesEntity
import com.yunquan.ohana.ui.home.frames.dialog.InvitationCodeDialog
import com.yunquan.ohana.ui.scan.ScanActivity

class EventSyncDialog(
    val context: AppCompatActivity,
    val activityResult: ActivityResultLauncher<Intent>,
    val devices: MutableList<FramesEntity>,
) : BaseDialog<DialogSyncToDeviceBinding>(context) {

    private var mAdapter: ModelRecyclerAdapter<FramesEntity>? = null

    private val mCheckMap = mutableMapOf<Int, FramesEntity>()

    private var onSelectDeviceListener: ((devices: List<FramesEntity>) -> Unit)? = null
    private var onBindDeviceListener: ((devicesCode: String) -> Unit)? = null

    fun setOnSelectDeviceListener(listener: (devices: List<FramesEntity>) -> Unit) {
        onSelectDeviceListener = listener
    }

    fun setOnBindDeviceListener(listener: (devicesCode: String) -> Unit) {
        onBindDeviceListener = listener
    }

    override fun createViewBinding(): DialogSyncToDeviceBinding {
        return DialogSyncToDeviceBinding.inflate(layoutInflater)
    }

    override fun cancelable(): Boolean = true

    override fun setGravity(): Int = Gravity.BOTTOM

    override fun setAnimRes(): Int = R.style.BottomAnimStyle

    override fun initData() {
        mAdapter = ModelRecyclerAdapter(context, devices, R.layout.item_sync_device)
        mAdapter!!.setBindViewListener(object : ModelRecyclerAdapter.ModelBindViewListener<FramesEntity> {
            override fun onBindView(holder: ModelViewHolder, item: FramesEntity, position: Int) {
                holder.setText(R.id.tv_status, item.roomLocation ?: "")
                    .setText(R.id.tv_profile_name, item.name ?: "")
                val checkBox = holder.getViewAtId<RCheckBox>(R.id.cb_select)
                checkBox.isChecked = mCheckMap.containsKey(position)
                checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                    if (isChecked) {
                        mCheckMap[position] = item
                    } else {
                        mCheckMap.remove(position)
                    }
                }
            }
        })

        binding.rvDevice.layoutManager = LinearLayoutManager(context)
        binding.rvDevice.adapter = mAdapter
    }

    override fun initView() {
        if (devices.isNotEmpty()) {
            binding.llRv.visibility = View.VISIBLE
            binding.tvSync.visibility = View.VISIBLE
            binding.tipNoDevice.visibility = View.GONE
            binding.llAddFrames.visibility = View.GONE
        } else {
            binding.llRv.visibility = View.GONE
            binding.tvSync.visibility = View.GONE
            binding.tipNoDevice.visibility = View.VISIBLE
            binding.llAddFrames.visibility = View.VISIBLE
        }
        binding.btnScan.setOnClickListener(this)
        binding.btnInvitationCode.setOnClickListener(this)
        binding.tvSync.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_sync -> {
                val list = mCheckMap.values.toList()
                onSelectDeviceListener?.invoke(list)
                dismiss()
            }

            R.id.btn_scan -> {
                val intent = Intent(context, ScanActivity::class.java)
                activityResult.launch(intent)
                dismiss()
            }

            R.id.btn_invitation_code -> {
                val dialog = InvitationCodeDialog(context)
                dialog.setOnCallback(object : InvitationCodeDialog.OnCallback {
                    override fun call(code: String) {
                        onBindDeviceListener?.invoke(code)
                        dismiss()
                    }
                })
                dialog.show()
            }
        }
    }
}